﻿using NUnit.Framework;
using Framework.Extensions;
using System.Linq;

namespace Framework.UnitTest.Extensions
{
    [TestFixture]
    public class TypeExtensionsFixture
    {
        [Test]
        public void GetImplementedTypesReturnsAllBaseClassesAndAllInterfaces()
        {
            var baseArguments = typeof (TypeTestData)
                .GetImplementedTypes()
                .ToList();

            Assert.AreEqual(4, baseArguments.Count);

            Assert.IsTrue(baseArguments.Contains(typeof(TypeTestDataBase<string>)));
            Assert.IsTrue(baseArguments.Contains(typeof(ITestInterface1)));
            Assert.IsTrue(baseArguments.Contains(typeof(ITestInterface2)));
            Assert.IsTrue(baseArguments.Contains(typeof(object)));
        }

        [Test]
        public void GetArgumentsOfGenericBaseTypeReturnsArgumentFromAnyImplementedTypeMatchingArgument()
        {
            var baseArguments = typeof (TypeTestData)
                .GetArgumentsOfGenericBaseType(typeof (TypeTestDataBase<>));

            Assert.AreEqual(typeof(string), baseArguments.Single());
        }
    }

    public interface ITestInterface1
    {}

    public interface ITestInterface2
    {}

    public class TypeTestDataBase<T> : ITestInterface2
    {}

    public class TypeTestData : TypeTestDataBase<string>, ITestInterface1
    {}
}